<template>
	<view>
		<view class="" style="background-color: #fff;">
			<view class="" style="margin-top: 20upx;">
				<!-- <view class="" style="height: 100upx;line-height: 100upx; width: 100%;margin-left: 150upx;font-weight: 550;font-size: 35upx;">
					挂卖昵称 : {{buy.nick_name}}
				</view> -->
				<view class="" style="height: 100upx;line-height: 100upx;width: 100%;margin-left: 150upx;font-weight: 550;font-size: 35upx;">
					当前算力值剩余兑换量 : {{s_exchange}}
				</view>
				<view class="" style="height: 100upx;line-height: 100upx;width: 100%;margin-left: 150upx;font-weight: 550;font-size: 35upx;">
					已释放易豆值 : {{bz_release}}
				</view>
				<view class="" style="display: flex; height: 100upx;line-height: 100upx;width: 100%;margin-left: 150upx;font-weight: 550;font-size: 35upx;">
					<view class="" style=" width: 20%;height: 100upx;line-height: 100upx;font-size: 35upx;">
						购买数量:
					</view>
					<input type="text" v-model="num" style="margin-left: 100upx; width: 20%;height: 80upx;line-height: 80upx;box-shadow: 2upx 2upx 10upx #CCCCCC;" />
				</view>
				
			</view>
		</view>
		<button @click="getbuy" type="default" style="width: 90%;margin-left: 5%;background-color: #05052B;margin-top: 30upx;color: #fff;font-weight: 550;">立即兑换</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
			 buy:'',
			 znum:'',
			 price:'',
			 snum:'',
			 openid:'',
			 num:'',
			 bz_release:'',
			 s_exchange:'',
			 zichan_power:'',
			 nzichan_power:'',
			};
		},
		onLoad:function(){
			// uni.getStorage({
			//     key: 'buy',
			//     success: (res)=> {
			// 			console.log(res)
			//        this.buy = res.data
			// 			 this.znum = this.buy.znum
			// 			 this.price = this.buy.price
			// 			 this.id = this.buy.id
			//     }
			// });
			uni.getStorage({
			    key: 'openid',
			    success: (res)=> {
			       this.openid = res.data
						 console.log(this.openid)
						 this.getdata()
			    }
			});
		},
		methods:{
			getdata() {
				uni.request({
					url: 'http://mall.zhongzhengyicai.com/app/index.php?i=2&c=entry&m=ewei_shopv2&do=mobile&r=member.bz_exchange&app=1',
					method: 'POST',
					data: {
						'openid':this.openid,
					},
					header:{'Content-Type':'application/x-www-form-urlencoded'},
					success:(res) => {
						console.log(res)
						this.snum = res.data.snum
						this.s_exchange = res.data.s_exchange
						this.bz_release = res.data.bz_release
						this.zichan_power = res.data.zichan_power
						this.nzichan_power = res.data.nzichan_power
						
					},
					fail: () => {},
					complete: () => {}
				});
			},
			getbuy() {
				let o = this
				var num1=o.num-o.s_exchange
				var price = num1*o.nzichan_power
				var total = o.s_exchange*o.zichan_power + num1*o.nzichan_power
				if(o.num>o.s_exchange){
					uni.showModal({
						title: '提示',
						content:'当前轮次最多以算力'+o.zichan_power+'购买'+o.s_exchange+'易豆'+','+'还有'+num1+'易豆将以'+o.nzichan_power+'算力购买,需要花费'+price+'易豆值',
						success: function (res) {
							if (res.confirm) {
								uni.showModal({
								    title: '提示',
								    content: '本次兑换需要消耗'+ total +'个易通值',
								    success: function (res) {
								        if (res.confirm) {
								            console.log(o.num);
														uni.request({
															url: 'http://mall.zhongzhengyicai.com/app/index.php?i=2&c=entry&m=ewei_shopv2&do=mobile&r=member.bz_exchange.submit&app=1',
															method: 'POST',
															data: {
																'openid':o.openid,
																'num' : o.num,
															},
															header:{'Content-Type':'application/x-www-form-urlencoded'},
															success:(res) => {
																console.log(res)
																uni.showToast({
																	title:res.data.result.message,
																	icon:'none'
																})
																setTimeout(()=> {
																	uni.navigateTo({
																		url:'bz'
																	})
																}, 1000);
																
															},
															fail: () => {},
															complete: () => {}
														});
								        } else if (res.cancel) {
								            console.log('用户点击取消');
								        }
								    }
								});
							}
						}
					})
					
				}else{
					uni.showModal({
					    title: '提示',
					    content: '当前兑换需要消耗'+ o.num*o.zichan_power +'个易通值',
					    success: function (res) {
					        if (res.confirm) {
					            console.log(o.num);
											uni.request({
												url: 'http://mall.zhongzhengyicai.com/app/index.php?i=2&c=entry&m=ewei_shopv2&do=mobile&r=member.bz_exchange.submit&app=1',
												method: 'POST',
												data: {
													'openid':o.openid,
													'num' : o.num,
												},
												header:{'Content-Type':'application/x-www-form-urlencoded'},
												success:(res) => {
													console.log(res)
													uni.showToast({
														title:res.data.result.message,
														icon:'none'
													})
													setTimeout(()=> {
														uni.navigateTo({
															url:'bz'
														})
													}, 1000);
													
												},
												fail: () => {},
												complete: () => {}
											});
					        } else if (res.cancel) {
					            console.log('用户点击取消');
					        }
					    }
					});
				}
				
				
			},
		
		},
	}
</script>

<style>
</style>

